package com.wcy.bysj.controller;


import com.wcy.bysj.common.*;
import com.wcy.bysj.entity.User;
import com.wcy.bysj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user) {
        try{
            User user1 = userService.login(user);
            if (EmptyUtil.isEmpty(user1)) {
                return Result.error(ResultEnum.ERROR.getCode(),"密码错粗");
            }
            return Result.success(user1);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"登录接口异常");
        }
        catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"登录接口异常");
        }
    }

    @PostMapping("/regist")
    @ResponseBody
    public Result regist(@RequestBody User user) {
        try{
            return userService.regist(user);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"注册失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"注册失败");
        }
    }

    @PostMapping("/getUserList")
    @ResponseBody
    public Result getUserList(@RequestBody User user) {
        try{
            Page page = userService.getUserList(user);
            return Result.success(page);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"注册失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"注册失败");
        }
    }


    @PostMapping("/getUserByName")
    @ResponseBody
    public Result selectUserByName(@RequestBody User user) {
        try{
            Page page = userService.selectUserByName(user);
            return Result.success(page);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        }
    }

    @PostMapping("/deleteUser")
    @ResponseBody
    public Result deleteUser(@RequestBody User user) {
        try{
            return userService.deleteUser(user);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        }
    }


    @PostMapping("/updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody User user) {
        try{
            return userService.updateUser(user);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        }
    }

    @PostMapping("/addUser")
    @ResponseBody
    public Result addUser(@RequestBody User user) {
        try{
            return userService.addUser(user);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        }
    }

    @PostMapping("/logout")
    @ResponseBody
    public Result logout() {
        return Result.success("成功");
    }


    @PostMapping("/getUserFood")
    @ResponseBody
    public Result getUserFood(@RequestBody User user) {
        try{
            return userService.getUserFood(user);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"查询失败");
        }
    }
}

